package mainWindowSupport;

import java.util.Enumeration;
import java.util.List;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import actionHandlers.ShapeTreeSelectListener;
import cuShapes.*;
import palette.CanvasManager;

public class ShapeTree {

	JTree shapeTree;
	DefaultMutableTreeNode shapesRoot;
	DefaultMutableTreeNode arrowsRoot;
	DefaultMutableTreeNode rootCollection;
	
	public ShapeTree() {
		shapesRoot = new DefaultMutableTreeNode("Shapes",true);
		arrowsRoot = new DefaultMutableTreeNode("Arrows",true);
		rootCollection = new DefaultMutableTreeNode("Collection",true);
		shapeTree = new JTree(rootCollection);
		rootCollection.add(shapesRoot);
		rootCollection.add(arrowsRoot);
		shapeTree.updateUI();
		shapeTree.addMouseListener(new ShapeTreeSelectListener());
	}
	
	public JTree getTree() {
		return shapeTree;
	}
	
	public void addNode(PQShape shape) {
		DefaultMutableTreeNode tempShape = null;
		
		tempShape = new DefaultMutableTreeNode(shape);
		shapesRoot.add(tempShape);
		shapeTree.updateUI();
	}
	

	@SuppressWarnings("unchecked")
	public void setSelectedNode(PQShape shape) {
		DefaultMutableTreeNode tempNode = null;
		if(shape instanceof Box || shape instanceof Ellipse) {
			for(Enumeration<DefaultMutableTreeNode> e = shapesRoot.breadthFirstEnumeration(); e.hasMoreElements();) {
				tempNode = e.nextElement();
				if(tempNode.getUserObject().equals(shape)) {
					shapeTree.setSelectionPath(new TreePath(tempNode.getPath()));
					break;
				}
			}
		} else if (shape instanceof Arrow) {
			for(Enumeration<DefaultMutableTreeNode> e = arrowsRoot.breadthFirstEnumeration(); e.hasMoreElements();) {
				tempNode = e.nextElement();
				if(tempNode.getUserObject().equals(shape)) {
					shapeTree.setSelectionPath(new TreePath(tempNode.getPath()));
					break;
				}
			}
		}
	}
	
	public void removeNode(DefaultMutableTreeNode node) {
		shapesRoot.remove(node);
		shapeTree.updateUI();
	}
	
	public void formTreeFromList(List<PQShape> nodes) {
		DefaultMutableTreeNode node = null;
		
		if(nodes.get(0) instanceof Box || nodes.get(0) instanceof Ellipse) {
			shapesRoot.removeAllChildren();
			for(PQShape x : nodes) {
				node = new DefaultMutableTreeNode(x);
				shapesRoot.add(node);
			}
		} else if (nodes.get(0) instanceof Arrow) {
			arrowsRoot.removeAllChildren();
			for(PQShape x : nodes) {
				node = new DefaultMutableTreeNode(x);
				arrowsRoot.add(node);
			}
		}
		shapeTree.updateUI();
	}
	
	public void addFromOpen() {
		clear();
	    DefaultMutableTreeNode shape = null;
	    
	    for(PQShape x : CanvasManager.getInst().getShapeList()) {
	    	shape = new DefaultMutableTreeNode(x);
	    	shapesRoot.add(shape);
	    }
	    
	    for(PQShape x : CanvasManager.getInst().getArrowList()) {
	    	shape = new DefaultMutableTreeNode(x);
	    	arrowsRoot.add(shape);
	    }
	    shapeTree.updateUI();
	}
	
	public void clear() {
		rootCollection.removeAllChildren();
		shapeTree.updateUI();
	}
}
